package club.easin.sq.bl;

import club.easin.MYException;
import club.easin.common.DatagridRequestDto;
import club.easin.enums.Exenum;
import club.easin.enums.PwFinal;
import club.easin.pow.dto.UserSession;
import club.easin.sq.dao.SqCheckMapper;
import club.easin.sq.dao.SqMaintainMapper;
import club.easin.sq.dto.MaintainDetailDto;
import club.easin.sq.dto.MaintainQueryDto;
import club.easin.sq.dto.SqCheck;
import club.easin.sq.dto.SqMaintain;
import club.easin.utils.*;
import com.alibaba.druid.sql.visitor.functions.Now;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class Maintainbl {
    @Autowired
    private SqMaintainMapper sqMaintainMapper;
    @Autowired
    private DBSubmit dbSubmit;
    @Autowired
    private SqCheckMapper sqCheckMapper;

    public ReturnDto addManintain(UserSession userSession, SqMaintain sqMaintain){
        Tlist tlist=new Tlist();
        String userid=userSession.getUser().getUserid();
        Date now= DateUtils.getNow();
        SqCheck sqCheck=new SqCheck(Commutils.buildId(), PwFinal.nocheck,null,null,now,null);
        sqMaintain.setCheckid(sqCheck.getCheckid());
        sqMaintain.setFixid(Commutils.buildId());
        sqMaintain.setCreatetime(now);
        sqMaintain.setUpdatetime(now);
        sqMaintain.setCreateuser(userSession.getUser().getUserid());
        tlist.add(SqMaintainMapper.class,"insert",sqMaintain);
        tlist.add(SqCheckMapper.class,"insert",sqCheck);
        dbSubmit.exce(tlist);
        return ReturnUtils.returnsuccess();
    }
     public ReturnDto delManintain(String maintainid){
        Tlist tlist=new Tlist();
        String[] ids=maintainid.split(",");
        for(String id:ids){
            SqMaintain sqMaintain=sqMaintainMapper.selectByPrimaryKey(id);
            tlist.add(SqMaintainMapper.class,"deleteByPrimaryKey",id);
            tlist.add(SqCheck.class,"deleteByPrimaryKey",sqMaintain.getCheckid());
        }
        dbSubmit.exce(tlist);
        return ReturnUtils.returnsuccess();
     }

     public ReturnDto updateManintain(SqMaintain sqMaintain){
        Tlist tlist=new Tlist();
        SqMaintain maintain=sqMaintainMapper.selectByPrimaryKey(sqMaintain.getFixid());
        if(maintain!=null){
            SqCheck sqCheck=sqCheckMapper.selectByPrimaryKey(maintain.getCheckid());
            if(sqCheck!=null){
                if(!PwFinal.nocheck.equals(sqCheck.getState())){
                    throw new MYException(Exenum.IS_CHECK);
                }
                sqMaintain.setUpdatetime(DateUtils.getNow());
                tlist.add(SqMaintainMapper.class,"updateByPrimaryKeySelective",sqMaintain);
            }
        }
        dbSubmit.exce(tlist);
        return ReturnUtils.returnsuccess();
     }

     public ReturnDto getMaintainGrild(DatagridRequestDto<MaintainQueryDto> requestDto){
         Page<MaintainDetailDto> page=null;
         PageHelper.startPage(requestDto.getPagenum(),requestDto.getPagesize());
         MaintainQueryDto dto=requestDto.getData();
         page=sqMaintainMapper.getManintainGrild(dto);
         return ReturnUtils.returnsuccess(DataGrildUtils.returnDataGrild(page));
     }

     public ReturnDto mainCheckPass(UserSession userSession,String mainid){
        String userid=userSession.getUser().getUserid();
        SqMaintain maintain=sqMaintainMapper.selectByPrimaryKey(mainid);
         if(maintain!=null){
             SqCheck sqCheck=sqCheckMapper.selectByPrimaryKey(maintain.getCheckid());
             sqCheck.setCheckuser(userid);
             mainCheck(sqCheck,PwFinal.checkpass);
         }
         return ReturnUtils.returnsuccess();
     }

    public ReturnDto mainCheckNoPass(UserSession userSession,String mainid){
        String userid=userSession.getUser().getUserid();
        SqMaintain maintain=sqMaintainMapper.selectByPrimaryKey(mainid);
        if(maintain!=null){
            SqCheck sqCheck=sqCheckMapper.selectByPrimaryKey(maintain.getCheckid());
            sqCheck.setCheckuser(userid);
            mainCheck(sqCheck,PwFinal.nocheckpass);
        }
        return ReturnUtils.returnsuccess();
    }


     public boolean mainCheck(SqCheck sqCheck,String checkstate){
         Tlist tlist=new Tlist();
         if(sqCheck!=null){
             if(!PwFinal.nocheck.equals(sqCheck.getState())){
                 throw new MYException(Exenum.IS_CHECK);
             }sqCheck.setState(checkstate);
             sqCheck.setChecktime(DateUtils.getNow());
             sqCheckMapper.updateByPrimaryKeySelective(sqCheck);
             tlist.add(SqCheckMapper.class,"updateByPrimaryKeySelective",sqCheck);
         }
         dbSubmit.exce(tlist);
         return true;
     }


}
